home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / Libraries / SAT 2.3b4 / Demo ƒ / myPlatform demo ƒ / sPlatform.c < prev    next >
Text File  |  1995-01-17  |  2KB  |  102 lines

  1. /* Platform sprite, experimental faceless sprite */
  2.  
  3. #include "SAT.h"
  4. #include "myPlatform.h"
  5.  
  6. void InitPlatform()
  7. {
  8. /* nada*/
  9. }
  10.  
  11. pascal void SetupPlatform(SpritePtr me)
  12. {
  13.     Rect            r;
  14.     PolyHandle    pol;
  15.     
  16.     me->task = &HandlePlatform;
  17.     me->hitTask = &HitPlatform;
  18.  
  19.     me->face = nil; /* = faceless! */
  20.     SetRect(&(me->hotRect), 0, 0, 100, 16);
  21.     r = me->hotRect;
  22.     OffsetRect(&r, me->position.h, me->position.v);
  23.     SATSetPortBackScreen();
  24.     FillRect(&r, dkGray);
  25.  
  26.     pol = OpenPoly();
  27.     MoveTo(r.left, r.top);
  28.     LineTo(r.left + 5, r.top - 5);
  29.     LineTo(r.right + 5, r.top - 5);
  30.     LineTo(r.right, r.top);
  31.     LineTo(r.left, r.top);
  32.     LineTo(r.right, r.top);
  33.  
  34.     LineTo(r.right, r.bottom);
  35.     LineTo(r.right + 5, r.bottom - 5);
  36.     LineTo(r.right + 5, r.top - 5);
  37.     LineTo(r.right, r.top);
  38.  
  39.     ClosePoly();
  40.     ErasePoly(pol);
  41.     FramePoly(pol);
  42.     KillPoly(pol);
  43.  
  44.     r.top = r.top - 5;
  45.     r.right = r.right + 5;
  46.     SATBackChanged(&r); /* Tell SAT to draw it when appropriate */
  47.  
  48.     me->layer = -me->position.v;
  49. }
  50.  
  51. pascal void HandlePlatform(SpritePtr me)
  52. {
  53.     /*me->face = nil;*/ 
  54. }
  55.  
  56. pascal void HitPlatform(SpritePtr me, SpritePtr him)
  57. {
  58.     int    mini, i, min;
  59.     int    diff[5];
  60.     
  61.     if (him->task != HandlePlatform){
  62.         diff[1] = -me->hotRect2.top + (him->hotRect2.bottom);        /* TtoB */
  63.         diff[2] = -him->hotRect2.top + (me->hotRect2.bottom);         /* BtoT */
  64.         diff[3] = -me->hotRect2.left + (him->hotRect2.right);        /* LtoR */
  65.         diff[4] = -him->hotRect2.left + (me->hotRect2.right);        /* RtoL */
  66.         mini = 0;
  67.         min = 10000;
  68.         for(i = 1; i <= 4 ; i++){
  69.             if(min > diff[i]){
  70.                 min = diff[i];
  71.                 mini = i;
  72.             } /* if */
  73.         }
  74.         switch(mini){
  75.             case 1: /*floor*/
  76.                     him->position.v = him->position.v - diff[1] + 1; 
  77.                     him->kind = 10; /* Signal to him, as if we used KindCollision */
  78.                     if(him->speed.v > 0)
  79.                         him->speed.v = 0;
  80.                     break;
  81.             case 2: /* ceiling */
  82.                     him->position.v = him->position.v + diff[2] + 1;
  83. /*We don't signal here. A hit in the ceiling should just send him back down again.*/
  84.                     if(him->speed.v < 0)
  85.                         him->speed.v = -him->speed.v;
  86.                     break;
  87.             case 3: /*left*/
  88.                     him->position.h = him->position.h - diff[3] - 1;
  89.                     him->kind = 10;  /* Signal to him, as if we used KindCollision */
  90.                     if(him->speed.h > 0)
  91.                         him->speed.h = -him->speed.h;
  92.                     break;
  93.             case 4: /*right*/
  94.                     him->position.h = him->position.h + diff[4] + 1;
  95.                     him->kind = 10;  /* Signal to him, as if we used KindCollision */
  96.                     if(him->speed.h < 0)
  97.                         him->speed.h = -him->speed.h;
  98.                     break;
  99.         } /* switch */
  100.     }
  101. }
  102.